function reportFormat = getReportFormatConfig(reportType, environment)
%getReportFormatConfig Get report format from project configuration
%   Get the report format for a specific report type and environment
%   from the project configuration file.
%
%   reportFormat = getReportFormatConfig(reportType, environment)
%
%   Inputs:
%       reportType - String specifying the type of report
%                   ('model standards', 'code standards', 'SDD report', etc.)
%       environment - String specifying the environment ('CI' or 'DEV')
%
%   Output:
%       reportFormat - String specifying the format ('html' or 'pdf')

%   

% Default values
if nargin < 2 || isempty(environment)
    environment = 'DEV'; % Default to DEV environment
end

% Read project configuration for report format from YAML file
% 使用YAML配置文件替代JSON
% 通过 ProjectPathManager 获取项目根目录，确保路径一致性
rootDir = ProjectPathManager.getRootDir();
configFilePath = fullfile(rootDir, 'project_configuration', 'project_config.yml');

% 检查配置文件是否存在
if ~exist(configFilePath, 'file')
    error('getReportFormatConfig:ConfigFileNotFound', ...
        '配置文件不存在: %s\n请确保项目配置文件存在。', configFilePath);
end

% 读取配置文件
try
    prjConfig = yamlread(configFilePath);
catch ME
    error('getReportFormatConfig:ConfigFileReadError', ...
        '读取配置文件失败: %s\n错误信息: %s', configFilePath, ME.message);
end

% 验证返回类型是否为结构体
if ~isstruct(prjConfig)
    error('getReportFormatConfig:InvalidConfigFormat', ...
        ['配置文件解析结果不是结构体: %s\n' ...
         '返回类型: %s\n' ...
         '这可能是因为：\n' ...
         '1. YAML 文件格式有误\n' ...
         '2. yamlread 函数未正确加载\n' ...
         '3. 配置文件内容为空或格式错误\n' ...
         '请检查配置文件格式是否正确。'], ...
        configFilePath, class(prjConfig));
end

% 检查ReportFormat字段是否存在（大小写不敏感）
fieldNames = fieldnames(prjConfig);
reportFormatFieldIdx = find(strcmpi(fieldNames, 'ReportFormat'), 1);

if isempty(reportFormatFieldIdx)
    % 提供详细的字段信息帮助调试
    availableFields = strjoin(fieldNames, ', ');
    error('getReportFormatConfig:MissingReportFormatField', ...
        ['配置文件中缺少 ReportFormat 字段: %s\n' ...
         '配置文件中存在的字段: %s\n' ...
         '请在配置文件中添加 ReportFormat 配置。'], ...
        configFilePath, availableFields);
end

% 使用实际的字段名（可能与大小写不同）
actualFieldName = fieldNames{reportFormatFieldIdx};
if isempty(prjConfig.(actualFieldName))
    error('getReportFormatConfig:EmptyReportFormatField', ...
        '配置文件中的 %s 字段为空: %s\n请在配置文件中添加 ReportFormat 配置内容。', ...
        actualFieldName, configFilePath);
end

reportFormatConfig = prjConfig.(actualFieldName);

% Map report type to configuration key
configKey = getConfigKey(reportType);

% Get configuration for this report type
if ~isfield(reportFormatConfig, configKey)
    error('getReportFormatConfig:ReportTypeNotConfigured', ...
        '报告类型 "%s" (配置键: %s) 在配置文件中未找到。\n请在 project_config.yml 的 ReportFormat 字段中添加该报告类型的配置。', ...
        reportType, configKey);
end

typeConfig = reportFormatConfig.(configKey);

% Get format for specified environment
if ~isfield(typeConfig, environment)
    error('getReportFormatConfig:EnvironmentNotConfigured', ...
        '报告类型 "%s" 的环境配置 "%s" 未找到。\n请在 project_config.yml 中为该报告类型添加 %s 环境的配置。', ...
        reportType, environment, environment);
end

reportFormat = typeConfig.(environment);

end

% Helper function to map report type to configuration key
function configKey = getConfigKey(reportType)
    switch lower(reportType)
        case {'sdd report', 'sdd', 'sdd_report'}
            configKey = 'sdd_report';
        case {'model standards', 'modelstds', 'model_standards'}
            configKey = 'model_standards';
        case {'model metrics', 'modelmetrics', 'model_metrics'}
            configKey = 'model_metrics';
        case {'design error detection', 'designerrs', 'design_error_detection'}
            configKey = 'design_error_detection';
        case {'code as top', 'codeastop', 'code_as_top'}
            configKey = 'code_as_top';
        case {'code as ref', 'codeasref', 'code_as_ref'}
            configKey = 'code_as_ref';
        case {'code inspector', 'codeinspector', 'code_inspector'}
            configKey = 'code_inspector';
        case {'code standards', 'codestds', 'code_standards'}
            configKey = 'code_standards';
        case {'code metrics', 'codemetrics', 'code_metrics'}
            configKey = 'code_metrics';
        case {'bug finder', 'bugfinder', 'coding errors', 'bug_finder'}
            configKey = 'bug_finder';
        case {'data flow defects', 'dataflow', 'data_flow_defects'}
            configKey = 'data_flow_defects';
        case {'code quality', 'codequality', 'prove', 'code_quality'}
            configKey = 'code_quality';
        case {'requirements', 'req'}
            configKey = 'requirements';
        otherwise
            configKey = 'default';
    end
end 